//
/**
 * Filename: fig10-10_timeout_read.c
 * Author:   csbonkers
 * Contact:  forever65535@qq.com
 * Date:     2021/6/19
 * Description:
 *    带时间限制地read操作
 */

#include "apue.h"

 static void sig_alrm(int);
int main(int argc, char *argv[]) {
  char line[MAXLINE];
  if(signal(SIGALRM,sig_alrm) == SIG_ERR){
    err_sys("signal(SIGALRM) error");
  }
  alarm(10);
  int n;
  if((n = read(STDIN_FILENO, line, MAXLINE)) < 0){
    err_sys("read error");
  }
  alarm(0);
  write(STDOUT_FILENO, line, n);
  return 0;
}

void sig_alrm(int signo){
  /*nothing to do, just return to interrupt the read*/
}
